package servlet;


import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import agent.AreaRotas;

import util.FuzzyUtil;

import control.BuscaRota;
import control.ControleAgentes;

import model.Coordenada;
import model.Rota;
import model.SubRota;

/**
 * Servlet implementation class ServletPrincipal
 */
@WebServlet({ "/ServletPrincipal", "/principal" })
public class ServletPrincipal extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ServletPrincipal() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//String comando = request.getParameter("comando");
		String msg = "";
		double origemLat = Double.parseDouble(request.getParameter("origemLat"));
		double origemLon = Double.parseDouble(request.getParameter("origemLon"));
		double destinoLat = Double.parseDouble(request.getParameter("destinoLat"));
		double destinoLon = Double.parseDouble(request.getParameter("destinoLon"));
		String descOrig = request.getParameter("descOrig");
		String descDest = request.getParameter("descDest");
		double distanciaPe = Double.parseDouble(request.getParameter("distancia"));
		if(origemLat != 0 && origemLon != 0 && destinoLat != 0 && destinoLon != 0){
			Coordenada origem = new Coordenada(origemLat, origemLon);
			Coordenada destino = new Coordenada(destinoLat, destinoLon);
			ArrayList<Rota> rotas = new ArrayList<Rota>();
			AreaRotas.setRotas(rotas);
			//ArrayList<SubRota> subRotas = new ArrayList<SubRota>();
			//BuscaRota busca = new BuscaRota(origem, destino);
			
			try{
				ControleAgentes ca = new ControleAgentes();
				ca.iniciaAgentes(origem, destino);
				while(AreaRotas.getNumRotas() < 4){
					System.out.println("Aguardando agentes");
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				//rotas = busca.buscar(origem, destino, subRotas);
				Rota rotaFuzzyficada = new Rota();
				FuzzyUtil fuzzy = new FuzzyUtil();
				rotaFuzzyficada = fuzzy.deFuzzy(AreaRotas.getRotas(), distanciaPe);
				rotaFuzzyficada.setOrigem(origem);
				rotaFuzzyficada.setDescOrig(descOrig);
				rotaFuzzyficada.setDestino(destino);
				rotaFuzzyficada.setDescDest(descDest);
				request.setAttribute("rota", rotaFuzzyficada);
				msg = "OK";
				request.setAttribute("msg", msg);
				RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
				rd.forward(request, response);
				ca.paraJade();
			}
			catch(NullPointerException e){
				msg = "Null pointer Exception, Corrigir busca";
				request.setAttribute("msg", msg);
				RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
				rd.forward(request, response);
			}
			
			
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
